package com.inkwell.internet.productregistration.registration.portlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.inkwell.internet.productregistration.model.PRProduct;
import com.inkwell.internet.productregistration.model.PRRegistration;
import com.inkwell.internet.productregistration.model.PRUser;
import com.inkwell.internet.productregistration.model.impl.PRRegistrationImpl;
import com.inkwell.internet.productregistration.model.impl.PRUserImpl;
import com.inkwell.internet.productregistration.service.PRProductLocalServiceUtil;
import com.inkwell.internet.productregistration.service.PRRegistrationLocalServiceUtil;
import com.inkwell.internet.productregistration.service.PRUserLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class ProductAdminPortlet extends MVCPortlet {

	protected String editProductJSP = "/admin/edit_product.jsp";
	protected String displayRegistrationsJSP = "/admin/display_registrations.jsp";
	protected String viewRegistrationJSP = "/admin/view_registration.jsp";
	private Log _log = LogFactory.getLog(ProductAdminPortlet.class);

	public void addProduct(ActionRequest request, ActionResponse response)
			throws Exception {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);

		PRProduct product = ActionUtil.productFromRequest(request);

		ArrayList<String> errors = new ArrayList<String>();

		boolean productValid = ProdRegValidator
				.validateProduct(product, errors);

		if (productValid) {
			PRProductLocalServiceUtil.addProduct(product,
					themeDisplay.getUserId());
			SessionMessages.add(request, "add-product-successfully");
		} else {
			SessionErrors.add(request, "fields-required");
		}

	}

	public void editProduct(ActionRequest request, ActionResponse response)
			throws Exception {
		long productKey = ParamUtil.getLong(request, "resourcePrimKey");

		if (Validator.isNotNull(productKey)) {
			PRProduct product = PRProductLocalServiceUtil
					.getPRProduct(productKey);
			request.setAttribute("product", product);
			response.setRenderParameter("jspPage", editProductJSP);
		}
	}

	public void updateProduct(ActionRequest request, ActionResponse response)
			throws Exception {
		long productKey = ParamUtil.getLong(request, "resourcePrimKey");

		if (Validator.isNotNull(productKey)) {
			ArrayList<String> errors = new ArrayList<String>();
			PRProduct updateProduct = PRProductLocalServiceUtil
					.getPRProduct(productKey);
			PRProduct requestProduct = ActionUtil.productFromRequest(request);

			if (ProdRegValidator.validateProduct(requestProduct, errors)) {
				updateProduct.setProductName(requestProduct.getProductName());
				updateProduct.setSerialNumber(requestProduct.getSerialNumber());
				PRProductLocalServiceUtil.updatePRProduct(updateProduct);
				SessionMessages.add(request, "update-product-successfully");
			} else {
				for (String error : errors) {
					SessionErrors.add(request, error);
				}
			}

		} else {
			SessionErrors.add(request, "error-updating");
		}
	}

	public void deleteProduct(ActionRequest request, ActionResponse response)
			throws Exception {
		long productKey = ParamUtil.getLong(request, "resourcePrimKey");

		if (Validator.isNotNull(productKey)) {
			PRProductLocalServiceUtil.deleteProduct(productKey);
			SessionMessages.add(request, "delete-product-successfully");
		} else {
			SessionErrors.add(request, "error-deleting");
		}
	}

	public void editDisplayRegistrations(ActionRequest request,
			ActionResponse response) throws PortletException, IOException {

		response.setRenderParameter("jspPage", displayRegistrationsJSP);
	}
	
	public void viewRegistration (ActionRequest request,
			ActionResponse response) throws PortletException, IOException {
		long primKey = ParamUtil.getLong(request, "resourcePrimKey");
		
		PRUser regUser = new PRUserImpl();
		PRRegistration registration = new PRRegistrationImpl();
		
		if(Validator.isNotNull(primKey)) {
			try {
				registration = PRRegistrationLocalServiceUtil.getPRRegistration(primKey);
				regUser = PRUserLocalServiceUtil.getPRUser(registration.getPrUserId());
			} catch (PortalException e) {
				_log.debug(e);
			} catch (SystemException e) {
				_log.debug(e);
			}
			
			request.setAttribute("regUser", regUser);
			request.setAttribute("registration", registration);
			response.setRenderParameter("jspPage", viewRegistrationJSP);
		}
	}
}
